﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Collections;
using System.Data;

namespace Catering.Catering_Page.system
{
    public partial class ClassInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.LoadData();
                this.LoadRep();
            }
        }
        //加载页面数据
        public void LoadData()
        {
            if (Session["UserID"] != null)
            {
                string userid = Session["UserID"].ToString();
                int companyid = new Catering_Rules.T_Personnel().GetCompanyIDByPID(Convert.ToInt32(userid));
                if (companyid > 0)
                {
                    DataTable dt_OperationArea = new Catering_Rules.OperationArea().GetAllListByCompanyID(companyid).Tables[0];
                    this.Drop_Operation.LoadDrop(dt_OperationArea, "id", "AreaName");
                }
            }

            DataTable dt_Area = new Catering_Rules.Area().GetAllList().Tables[0];
            this.Drop_AName.LoadDrop(dt_Area, "id", "Name");
        }

        //加载Rep
        public void LoadRep()
        {
            DataTable dt_OperationArea = new Catering_Rules.ClassInfo().GetAllList().Tables[0];
            this.Rep_OperationArea.DataSource = dt_OperationArea;
            this.Rep_OperationArea.DataBind();
        }

        //获取
        [WebMethod]
        public static ArrayList Get(string cid)
        {
            ArrayList al = new ArrayList();
            Catering_Model.ClassInfo md = new Catering_Rules.ClassInfo().GetModel(Convert.ToInt32(cid));
            al.Add(md.AreaID);
            al.Add(md.CName);
            al.Add(md.Back);
            al.Add(md.temp1);
            al.Add(md.AID);
            return al;
        }

        //添加
        [WebMethod]
        public static string Add(string AreaID, string CName, string Back, string temp1, string AID)
        {
            Catering_Model.ClassInfo md = new Catering_Model.ClassInfo();
            md.id = new Catering_Rules.ClassInfo().GetMaxId();
            md.AreaID = Convert.ToInt32(AreaID);
            md.CName = CName;
            md.Back = Back;
            md.temp1 = temp1;
            md.AID = Convert.ToInt32(AID);
            new Catering_Rules.ClassInfo().Add(md);
            return "OK";
        }

        //修改
        [WebMethod]
        public static string Edit(string cid, string AreaID, string CName, string Back, string temp1, string AID)
        {
            int id = 0;
            int areaid = 0;
            Int32.TryParse(cid, out id);
            Int32.TryParse(AreaID, out areaid);
            if (id <= 0) return "NO";
            if (areaid <= 0) return "NO";

            //Catering_Model.ClassInfo oldmd = new Catering_Rules.ClassInfo().GetModel(id);
            Catering_Model.ClassInfo md = new Catering_Model.ClassInfo();
            //if (areaid == oldmd.AreaID && CName == oldmd.CName && Back == oldmd.Back && temp1 == oldmd.temp1 && AID == oldmd.AID.ToString())
            //{
            //    return "OK";
            //}
            md.id = id;
            md.AreaID = areaid;
            md.CName = CName;
            md.Back = Back;
            md.temp1 = temp1;
            md.AID = Convert.ToInt32(AID);
            new Catering_Rules.ClassInfo().Update(md);
            //if (areaid != oldmd.AreaID)
            //{
                new Catering_Rules.ClassInfo().UpdateArearIDByClassID(areaid, id);
            //}
            return "OK";
        }

        //删除
        protected void Rep_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                int delID = Convert.ToInt32(e.CommandArgument);
                if (new Catering_Rules.ClassInfo().Delete(delID))
                {
                    this.LoadRep();
                }
            }
        }

        public string GetANameByID(string Strid)
        {
            int id = 0;
            Int32.TryParse(Strid, out id);
            if (id > 0)
            {
                Catering_Model.Area md = new Catering_Rules.Area().GetModel(id);
                if (md != null && !string.IsNullOrEmpty(md.Name)) return md.Name;
            }
            return "";
        }
    }
}